/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CapadeDatos;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import modelo.beanDepartamento;
import modelo.beanusuario;
import proyectocatalina.Login;
/**
 *
 * @author principal
 */
public class DaoDepartamento {

public static  ArrayList vertodoslosdepartamentos(){
        ArrayList lista = new ArrayList();
    try{
            Connection cn = ConexionBD.getInstance().getConnection();
            Statement stm = cn.createStatement();
            String sql ="Select * from departamento;"; // PROCEDIMIENTO ALMACENADO
            ResultSet rs =stm.executeQuery(sql);
            while (rs.next()){
                   beanDepartamento depar  = new beanDepartamento();
                   depar.setNom_depart(rs.getString(2));
                   depar.setCod_depart(rs.getString(1));
                   lista.add(depar);
        }
    }catch(Exception e){System.out.print(e);
   }return lista;
   }
    private beanusuario User;

public void insertardepartamento(String CodDeparta , String NomDeparta){
        Boolean verdad = false;
    try{
            Connection cn = ConexionBD.getInstance().getConnection();
            Statement stm = cn.createStatement();
            String sql ="Call procregistrardepartamento('"
                    + CodDeparta + "', '"
                    + NomDeparta +  "');";
            User = Login.usuarioglobal;
            String sql3 = "Call proc_auditoria(?,?,?,?,?,?)";
            Connection cn4 = ConexionBD.getInstance().getConnection();
            CallableStatement insertauditoria = cn4.prepareCall(sql3);
            insertauditoria.setInt(1, User.getIdUsuario());
            insertauditoria.setString(2, "Departamento");
            insertauditoria.setString(3, "insertar");
            insertauditoria.setString(4, "nulo");
            insertauditoria.setString(5,CodDeparta+", "+NomDeparta);
            insertauditoria.setString(6,"nulo");  
            if(stm.executeQuery(sql).wasNull()){
               verdad = false;
               JOptionPane.showMessageDialog(null,"NO INGRESADO");
            }
            else {
                verdad = true;
                insertauditoria.execute();
                JOptionPane.showMessageDialog(null,"INGRESADO");
            }
    }catch(Exception e){System.out.print(e);
   }
   }

}
